package exer2;

/**
 * @author tongchen
 * @create 2022-10-10 15:59
 */
public class PalindromeList {
    public static boolean chkPalindrome(ListNode A) {
        //首先处理链表为空的情况
        if(A==null){
            return false;
        }
        //通过快慢指针找到圆脸标的中间位置
        ListNode slow=A;
        ListNode fast=A;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        ListNode temp1=null;

        //分别处理奇数和偶数的情况
        if(fast==null) {

                temp1 = slow;
                ListNode pre=null;
                while (temp1 != null) {
                    ListNode next = temp1.next;
                    temp1.next=pre;
                    pre=temp1;
                    temp1=next;
                }
                ListNode temp3 = A;
                ListNode temp4 = pre;
                while (temp3 != slow) {
                    if (temp3.val == temp4.val) {
                        temp3 = temp3.next;
                        temp4 = temp4.next;
                    } else {
                        return false;
                    }
                }
                return true;
        }
        else{
            temp1 = slow;
            ListNode pre=null;
            while (temp1 != null) {
                ListNode next = temp1.next;
                temp1.next=pre;
                pre=temp1;
                temp1=next;

            }
            ListNode temp3=A;
            ListNode temp4=pre;
            while(temp3.next!=slow){
                if(temp3.val==temp4.val){
                    temp3=temp3.next;
                    temp4=temp4.next;
                }
                else{
                    return false;
                }
            }
            return true;

        }
    }
}
